/* app 登录页面 */

import 'package:flutter/material.dart';
import 'package:flutter_hcc/widget/outline_button.dart';

import 'passwd_page.dart';

class AMLoginPage extends StatefulWidget {
  BuildContext rootContext;
  AMLoginPage(this.rootContext);

  @override
  _AMLoginPageState createState() => _AMLoginPageState();
}

class _AMLoginPageState extends State<AMLoginPage> {
  final _focusNode = FocusNode();
  final _emailController = TextEditingController();

  @override
  void initState() {
    // 是否获取光标监听
    _focusNode.addListener(() {
      print("Login Has focus: ${_focusNode.hasFocus}");
    });
    _emailController.text = "tom@test.com";
    // 等待UI加载
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      FocusScope.of(context).requestFocus(_focusNode);
    });
  }

  @override
  void dispose() {
    _focusNode.dispose();
    _emailController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.fromLTRB(24, 5, 15, 20),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            alignment: Alignment.centerRight,
            child: TextButton(
              onPressed: exitAction,
              child: Text("退出"),
              style: ButtonStyle(
                backgroundColor: MaterialStateProperty.resolveWith((states) {
                  //设置按下时的背景颜色
                  if (states.contains(MaterialState.pressed)) {
                    return Colors.blue[200];
                  }
                  //默认不使用背景颜色
                  return Colors.orange[100];
                }),
                alignment: Alignment.centerRight,
              ),
            ),
          ),
          Text(
            "输入邮箱",
            style: TextStyle(
                color: Colors.black, fontSize: 24, fontWeight: FontWeight.w700),
          ),
          SizedBox(
            height: 10,
          ),
          Text(
            "账户可用是用户邮箱、安装商创建的admin邮箱地址或admin创建的子账户邮箱地址",
            style: TextStyle(
                color: Colors.black, fontSize: 12, letterSpacing: 0.5),
          ),
          TextField(
            enableSuggestions: false,
            controller: _emailController,
            focusNode: _focusNode,
            decoration: InputDecoration(
              hintText: "邮箱",
              enabledBorder: UnderlineInputBorder(
                borderSide: BorderSide(color: Color(0xFFBEBEBE)),
              ),
              focusedBorder: UnderlineInputBorder(
                borderSide: BorderSide(color: Color(0xFFBEBEBE)),
              ),
            ),
          ),
          SizedBox(
            height: 40,
          ),
          Container(
            child: BaseButton("下一步", pushPasswdPage),
            width: double.infinity,
            height: 44,
          )
        ],
      ),
    );

    FocusScope.of(context).requestFocus(_focusNode);
  }

  void exitAction() {
    /// 退出事件
    Navigator.pop(widget.rootContext);
  }

  void pushPasswdPage() {
    Navigator.of(context).push(MaterialPageRoute(builder: (context) {
      return AMPasswdPage(_emailController.text, widget.rootContext);
    }));
  }
}
